#include "camerawidget.h"
#include "ui_camerawidget.h"
#include <QDebug>
CameraWidget::CameraWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CameraWidget)
{
    ui->setupUi(this);
    this->setAttribute(Qt::WA_DeleteOnClose);
    int maxlen=INT_MIN;
    ui->btn_cancel->hide();
    ui->btn_recapture->hide();
    ui->btn_send->hide();
    nowbtn=ui->btn_photomode;
    anim=new QPropertyAnimation(ui->btn_photomode,"geometry");
    anim2=new QPropertyAnimation(ui->btn_videomode,"geometry");
    camera=new Camera();
    camera->initCamera(ui->label);
    camera->startCamera();
    //添加摄像头信息到combbox，同时计算最长字符串长度
    for(QCameraInfo info:camera->getCameraInfo())
    {
        QString infostr=info.description();
        maxlen=maxlen>infostr.length()?maxlen:infostr.length();
        ui->comboBox->addItem(info.description());
    }
    //调整combbox宽度
    ui->comboBox->resize(maxlen*9,ui->comboBox->height());
    connect(camera,&Camera::captureSuccessed,this,&CameraWidget::recvCameraImg);
}

CameraWidget::~CameraWidget()
{
    delete ui;
    camera->destroyCamera();
}

void CameraWidget::on_btn_videomode_clicked()
{
    //向左的切换动画
    if(nowbtn==ui->btn_videomode)return;
    else{
        anim->setDuration(500);
        anim->setStartValue(QRect(370,565,51,28));
        anim->setEndValue(QRect(320,565,51,28));
        anim2->setDuration(500);
        anim2->setStartValue(QRect(420,565,51,28));
        anim2->setEndValue(QRect(370,565,51,28));
        anim->start();
        anim2->start();
        nowbtn->setChecked(false);
        nowbtn=ui->btn_videomode;
    }
}

void CameraWidget::on_btn_photomode_clicked()
{
    //向右的切换动画
    if(nowbtn==ui->btn_photomode)return;
    else{
        anim->setDuration(500);
        anim->setStartValue(QRect(320,565,51,28));
        anim->setEndValue(QRect(370,565,51,28));
        anim2->setDuration(500);
        anim2->setStartValue(QRect(370,565,51,28));
        anim2->setEndValue(QRect(420,565,51,28));
        anim->start();
        anim2->start();
        nowbtn->setChecked(false);
        nowbtn=ui->btn_photomode;
    }
}

void CameraWidget::on_btn_startstop_clicked()
{
    //开始拍摄
    camera->captureImg();
    qDebug()<<"capture img";
}

void CameraWidget::recvCameraImg(QImage cameraimge)
{
    qDebug()<<"recv img";
    image=cameraimge;
    camera->stopCamera();
    ui->label->setPixmap(QPixmap::fromImage(image).scaled(ui->label->size()));
    ui->btn_photomode->hide();
    ui->btn_videomode->hide();
    ui->btn_startstop->hide();
    ui->comboBox->hide();
    ui->btn_cancel->show();
    ui->btn_recapture->show();
    ui->btn_send->show();
}

void CameraWidget::on_comboBox_currentIndexChanged(const int index)
{
    //切换摄像头
    //camera->setCamera(index);
}

void CameraWidget::on_btn_recapture_clicked()
{
    ui->btn_photomode->show();
    ui->btn_videomode->show();
    ui->btn_startstop->show();
    ui->comboBox->show();
    ui->btn_cancel->hide();
    ui->btn_recapture->hide();
    ui->btn_send->hide();
    camera->startCamera();
}

void CameraWidget::on_btn_cancel_clicked()
{
    this->close();
}

void CameraWidget::on_btn_send_clicked()
{
    emit sendCameraPhoto(image);
    qDebug()<<"C:\\Users\\Administrator\\Desktop\\"+camera->getImgName();
    image.save(QString("C:\\Users\\Administrator\\Desktop\\"+camera->getImgName()));
    this->close();
}
